#include <algorithm>
#include <iostream>
#include <vector>

using std::vector;

void print(int *arr) {
    for (int i = 0; i < 26; ++i)
        std::cout << arr[i] << ", ";
    std::cout << "\n";
}

class Solution {
public:
    int leastInterval(vector<char> &tasks, int n) {
        std::vector<int> cur(26);
        std::vector<int> waits(26);
        int zero_count = 0; // TODO:: 替代check zero
        for (int i = 0; i < 26; ++i) {
            cur[i] = -100;
            waits[i] = 0;
        }
        for (auto c : tasks) {
            ++waits[c - 'A'];
        }
        std::sort(waits.begin(), waits.end());
        int i = 0;
        while (!no_wait(waits)) {
            kill_wait(waits, cur, i, n);
            ++i;
        }
        return i;
    }

    void kill_wait(std::vector<int> &waits, std::vector<int> &cur, int i,
                   int n) {
        for (int j = 0; j < 26; ++j) {
            if (waits[j] > 0 and i - cur[j] > n) {
                --waits[j];
                cur[j] = i;
                std::cout << static_cast<char>(j + 'A') << ", ";
                return;
            }
        }
        std::cout << "wait, ";
    }

    bool no_wait(std::vector<int> &waits) {
        std::sort(waits.begin(), waits.end());
        for (int i = 0; i < 26; ++i) {
            if (waits[i] > 0)
                return false;
        }
        return true;
    }
};

int main() {
    Solution s;
    std::vector<char> fuck{'A', 'A', 'A', 'B', 'B', 'B'};
    std::cout << s.leastInterval(fuck, 2) << '\n';
}